package com.scm.converter;

import com.scm.common.core.domain.entity.SysUser;
import com.scm.common.enums.OrderOperationType;
import com.scm.common.enums.ProductSourceEnum;
import com.scm.common.utils.StringUtils;
import com.scm.common.utils.TOTransferHelper;
import com.scm.dto.req.order.OrderBeanReq;
import com.scm.dto.req.order.OrderImportBean;
import com.scm.common.utils.AmountUtils;
import com.scm.common.utils.DateUtils;
import com.scm.dto.resp.order.OrderBaobeiDo;
import com.scm.dto.resp.order.OrderDetailDo;
import com.scm.dto.resp.order.OrderPageDo;
import com.scm.dto.resp.order.ScmOrderProductDO;
import com.scm.persist.model.*;
import org.apache.commons.compress.utils.Lists;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class OrderConverter {

    public static List<OrderBeanReq> convertOMSOrdersToScmOrders(Long tenantId,List<OrderImportBean> orderImportModelList){

        List<OrderBeanReq> list = Lists.newArrayList();

        if(CollectionUtils.isEmpty(orderImportModelList)){
            return list;
        }

        Map<String, OrderBeanReq> mapOrder = new HashMap<>();

        for (OrderImportBean orderImportBean : orderImportModelList) {

            OrderBeanReq orderBean= mapOrder.get(orderImportBean.getOrderId());
            if(orderBean==null){
                ScmOrder scmOrder = convertOMSOrderToScmOrder(tenantId,orderImportBean);

                ScmOrderProduct scmOrderProduct = convertOMSOrderToScmOrderProduct(tenantId,orderImportBean);

                orderBean = new OrderBeanReq();
                orderBean.setOrder(scmOrder);

                List<ScmOrderProduct> orderProductList = Lists.newArrayList();
                orderProductList.add(scmOrderProduct);
                orderBean.setOrderProductList(orderProductList);

                mapOrder.put(orderImportBean.getOrderId(),orderBean);
            }else{

                ScmOrderProduct scmOrderProduct = convertOMSOrderToScmOrderProduct(tenantId,orderImportBean);
                List<ScmOrderProduct> orderProductList = orderBean.getOrderProductList();
                orderProductList.add(scmOrderProduct);
                orderBean.setOrderProductList(orderProductList);
                mapOrder.put(orderImportBean.getOrderId(),orderBean);

            }

        }

        list = mapOrder.values().stream().collect(Collectors.toList());

        return list;

    }


    public static ScmOrder convertOMSOrderToScmOrder(Long tenantId,OrderImportBean orderImportBean ) {

        ScmOrder scmOrder = new ScmOrder();
        scmOrder.setTenantId(tenantId);
//        scmOrder.setId();
        scmOrder.setOriginalOrderid(orderImportBean.getOriginalOrderId());
        scmOrder.setOrderid(orderImportBean.getOrderId());
        scmOrder.setOrderSource(ProductSourceEnum.OMS.getCode());
        scmOrder.setSupplierOrderid(orderImportBean.getSupplierOrderId());
        scmOrder.setSettlementNo(orderImportBean.getSettlementNo());

        scmOrder.setAmount(AmountUtils.yuanToFen(orderImportBean.getOrderAmount()));

        Date orderTime = DateUtils.parseDateByExcel(orderImportBean.getOrderTimeStr());
        if(orderTime!=null){
            scmOrder.setOrderTime(orderTime.getTime());
            scmOrder.setOrderTimeStr(DateUtils.dateTime2(orderTime));
        }

        scmOrder.setRecipientContact(orderImportBean.getRecipientContact());
        scmOrder.setOrderHolder(orderImportBean.getOrderHolder());
        scmOrder.setUserCode(orderImportBean.getUserCode());
        scmOrder.setMobile(orderImportBean.getMobile());
        scmOrder.setTel(orderImportBean.getTel());
        scmOrder.setAddress(orderImportBean.getAddress());
        scmOrder.setCompanyName(orderImportBean.getCompanyName());
        scmOrder.setUsername(orderImportBean.getUsername());
        scmOrder.setDept(orderImportBean.getDept());
        scmOrder.setReceiptNo(orderImportBean.getReceiptNo());
        scmOrder.setAdminName(orderImportBean.getAdminName());
        scmOrder.setProjectName(orderImportBean.getProjectName());
//        scmOrder.setProjectNameShort();
//        scmOrder.setProjectId();
        scmOrder.setRemark(orderImportBean.getRemark());

        Date deliveredTime = DateUtils.parseDateByExcel(orderImportBean.getDeliveredTimeStr());
        if(deliveredTime!=null){
            scmOrder.setDeliveredTime(deliveredTime.getTime());
            scmOrder.setDeliveredTimeStr(DateUtils.dateTime2(deliveredTime));
        }

        Date tuotouTime = DateUtils.parseDateByExcel(orderImportBean.getTuotouTimeStr());
        if(deliveredTime!=null){
            scmOrder.setTuotouTime(tuotouTime.getTime());
            scmOrder.setTuotouTimeStr(DateUtils.dateTime2(tuotouTime));
        }

        Date checkedTime = DateUtils.parseDateByExcel(orderImportBean.getCheckedTimeStr());
        if(checkedTime!=null){
            scmOrder.setCheckedTime(checkedTime.getTime());
            scmOrder.setCheckedTimeStr(DateUtils.dateTime2(checkedTime));
        }

        scmOrder.setInvoiceTitle(orderImportBean.getInvoiceTitle());
        scmOrder.setIfCancel(orderImportBean.getIfCancel());
        scmOrder.setIfSplitOrder(orderImportBean.getIfSplitOrder());
        scmOrder.setLogisticsName(orderImportBean.getLogisticsName());
        scmOrder.setLogisticsNo(orderImportBean.getLogisticsNo());

        Date logisticsFinishedTime = DateUtils.parseDateByExcel(orderImportBean.getLogisticsFinishedTimeStr());
        if(logisticsFinishedTime!=null){
            scmOrder.setLogisticsFinishedTime(logisticsFinishedTime.getTime());
            scmOrder.setLogisticsFinishedTimeStr(DateUtils.dateTime2(logisticsFinishedTime));
        }

        scmOrder.setOrderStatus(orderImportBean.getStatus());
        scmOrder.setOrderStatusDesc(orderImportBean.getStatus());


        return scmOrder;


    }


    public static ScmOrderProduct convertOMSOrderToScmOrderProduct(Long tenantId,OrderImportBean orderImportBean ) {

        ScmOrderProduct orderProduct = new ScmOrderProduct();

        orderProduct.setTenantId(tenantId);
        orderProduct.setProductCode(orderImportBean.getProductCode());
        orderProduct.setProductName(orderImportBean.getProductName());
        orderProduct.setProductSpec(orderImportBean.getProductSpec());
        orderProduct.setProductUnit(orderImportBean.getProductUnit());

        orderProduct.setProductPrice(AmountUtils.yuanToFen(orderImportBean.getProductPrice()));
        orderProduct.setAmount(AmountUtils.yuanToFen(orderImportBean.getAmount()));

        orderProduct.setProductCount(Integer.valueOf(orderImportBean.getProductCount()));
        if(StringUtils.isNotBlank(orderImportBean.getSendCount())){
            orderProduct.setSendCount(Integer.valueOf(orderImportBean.getSendCount()));
        }


        return orderProduct;

    }





    public static List<OrderPageDo> convertScmOrdersToOrderPages(List<ScmOrder> scmOrderList) {

        List<OrderPageDo> orderPageDoList = Lists.newArrayList();


        if(CollectionUtils.isEmpty(scmOrderList)){
            return orderPageDoList;
        }

        scmOrderList.forEach(scmOrder -> {

            orderPageDoList.add(convertSCMSOrdersToOrderPage(scmOrder));
        });

        return orderPageDoList;

    }


    public static OrderPageDo convertSCMSOrdersToOrderPage(ScmOrder scmOrder) {

        OrderPageDo orderPageDo = new OrderPageDo();
        orderPageDo.setScmOrderId(scmOrder.getId());
        orderPageDo.setOrderid(scmOrder.getOrderid());
        orderPageDo.setOrderSource(scmOrder.getOrderSource());

        orderPageDo.setProjectName(scmOrder.getProjectName());
        orderPageDo.setCompanyName(scmOrder.getCompanyName());
        orderPageDo.setSettlementNo(scmOrder.getSettlementNo());

        orderPageDo.setAmountStr(AmountUtils.fenToStr(scmOrder.getAmount()));

        orderPageDo.setOrderStatusDesc(scmOrder.getOrderStatusDesc());
        orderPageDo.setRecipientContact(scmOrder.getRecipientContact());
        orderPageDo.setAddress(scmOrder.getAddress());
        orderPageDo.setLogisticsNo(scmOrder.getLogisticsNo());


        return orderPageDo;

    }


    public static OrderDetailDo convertSCMSOrdersToOrderDetailDo(ScmOrder scmOrder, List<ScmOrderProduct> scmOrderProductList , ScmOrderOperation scmOrderOperation,ScmOrderInvoice scmOrderInvoice) {

        OrderDetailDo orderDetailDo = new OrderDetailDo();
        orderDetailDo.setScmOrderId(scmOrder.getId());
        orderDetailDo.setOrderid(scmOrder.getOrderid());
        orderDetailDo.setOrderSource(scmOrder.getOrderSource());

        orderDetailDo.setProjectName(scmOrder.getProjectName());
        orderDetailDo.setCompanyName(scmOrder.getCompanyName());
        orderDetailDo.setSettlementNo(scmOrder.getSettlementNo());

        orderDetailDo.setAmountStr(AmountUtils.fenToStr(scmOrder.getAmount()));

        orderDetailDo.setOrderStatusDesc(scmOrder.getOrderStatusDesc());
        orderDetailDo.setRecipientContact(scmOrder.getRecipientContact());
        orderDetailDo.setAddress(scmOrder.getAddress());
        orderDetailDo.setLogisticsNo(scmOrder.getLogisticsNo());

        orderDetailDo.setReceiptFile(scmOrderOperation.getReceiptFile());
        orderDetailDo.setUploadWaitReceiptFileTime(scmOrderOperation.getUploadWaitReceiptFileTime());
        orderDetailDo.setUploadWaitReceiptFileUser(scmOrderOperation.getUploadWaitReceiptFileUser());

        orderDetailDo.setScmOrderProductDOList(TOTransferHelper.trans2TO(scmOrderProductList,ScmOrderProductDO.class));
        orderDetailDo.setConfiretimestr(scmOrderOperation.getConfireTimeStr());
        orderDetailDo.setDeliveredTimeStr(scmOrder.getDeliveredTimeStr());
        orderDetailDo.setOrderTimeStr(scmOrder.getOrderTimeStr());
        orderDetailDo.setLogisticsFinishedTimeStr(scmOrder.getLogisticsFinishedTimeStr());
        orderDetailDo.setInvoiceStatus(scmOrder.getInvoiceStatus());
        orderDetailDo.setInvoicePath(scmOrderInvoice.getInvoiceFile());

        orderDetailDo.setReceiptFielStatus(scmOrder.getReceiptFielStatus());
        orderDetailDo.setWaitReceiptFile(scmOrderOperation.getWaitReceiptFile());

        return orderDetailDo;

    }


    public static List<OrderBaobeiDo> getAllWaitZhipaiUser(ScmOrder scmOrder,List<SysUser> sysUserList , List<ScmOrderBaobei> orderBaobeiList){

        if(CollectionUtils.isEmpty(sysUserList)){
            return Lists.newArrayList();
        }
        List<OrderBaobeiDo>  result = Lists.newArrayList();

        List<OrderBaobeiDo>  baobeiList = Lists.newArrayList();

        List<OrderBaobeiDo>  nobaobeiList = Lists.newArrayList();


        if(CollectionUtils.isEmpty(orderBaobeiList)){

            sysUserList.forEach(sysUser1 -> {
                OrderBaobeiDo orderBaobeiDo = getOrderBaobeiDo(scmOrder,sysUser1,null);
                nobaobeiList.add(orderBaobeiDo);
            });
        }else{

            Map<Long, SysUser> userMap = sysUserList.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));

            Map<String, ScmOrderBaobei> orderBaobeiMap = orderBaobeiList.stream().collect(Collectors.toMap(ScmOrderBaobei::getBaobeiren, Function.identity()));

            orderBaobeiList.forEach(scmOrderBaobei -> {
                SysUser sysUser = userMap.get(Long.valueOf(scmOrderBaobei.getBaobeiren()));
                OrderBaobeiDo orderBaobeiDo = getOrderBaobeiDo(scmOrder,sysUser,scmOrderBaobei);
                baobeiList.add(orderBaobeiDo);
            });

            sysUserList.forEach(sysUser1 -> {

                ScmOrderBaobei orderBaobei = orderBaobeiMap.get(sysUser1.getUserId().toString());
                if(orderBaobei==null){
                    OrderBaobeiDo orderBaobeiDo = getOrderBaobeiDo(scmOrder,sysUser1,null);
                    nobaobeiList.add(orderBaobeiDo);
                }else{
                    //已报备的人员已在baobeiList中维护
                }


            });

        }

        result.addAll(baobeiList);
        result.addAll(nobaobeiList);

        return result;

    }


    private static OrderBaobeiDo getOrderBaobeiDo(ScmOrder scmOrder,SysUser sysUser,ScmOrderBaobei orderBaobei){

        OrderBaobeiDo orderBaobeiDo = new OrderBaobeiDo();
        orderBaobeiDo.setScmOrderId(scmOrder.getId());
        orderBaobeiDo.setOrderid(scmOrder.getOrderid());
        orderBaobeiDo.setOrderSource(scmOrder.getOrderSource());
        orderBaobeiDo.setUserName(sysUser.getUserName());
        orderBaobeiDo.setUserId(sysUser.getUserId());
        if(orderBaobei==null){
            orderBaobeiDo.setDesc(sysUser.getUserName());
        }else{
            orderBaobeiDo.setDesc(sysUser.getUserName()+"，已于"+orderBaobei.getBaobeiTimeStr()+"报备");
        }
        return orderBaobeiDo;
    }


    public static ScmOrderLog createOrderLog(Long id, OrderOperationType orderOperationType, SysUser loginSysUser,String desc) {

        ScmOrderLog orderLog = new ScmOrderLog();
        orderLog.setScmOrderId(id);
        orderLog.setOperationType(orderOperationType.getCode());
        orderLog.setOperationTypeDesc(orderOperationType.getName());
        orderLog.setOperator(loginSysUser.getLoginName());
        orderLog.setOperatorId(String.valueOf(loginSysUser.getUserId()));
        orderLog.setCreateTime(System.currentTimeMillis());
        orderLog.setCreateUser(String.valueOf(loginSysUser.getUserId()));

        return orderLog;
    }

}
